home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14033 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  73 lines

  1. Path: portal.gmu.edu!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning address of value to main
  5. Date: 11 Apr 96 12:01:32 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.829224092@rscernix>
  8. References: <4khpjq$a3c@pipe1.nyc.pipeline.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=US-ASCII
  12. Content-Transfer-Encoding: 7bit
  13. X-Newsreader: NN version 6.5.0 #7 (NOV)
  14.  
  15. In <4khpjq$a3c@pipe1.nyc.pipeline.com> luciferm@nyc.pipeline.com (manjila thapa) writes:
  16.  
  17. >I want to return the address  of element of an array 
  18. >a[0]....a[n]....a[m-1]          (address of a[n] is to be returned)
  19. >from a function to the main so that:
  20. >
  21. >main(){
  22. >double *newarr;
  23. >...
  24. >...
  25. >newarr= function(......) /*function is supposed to return address*/
  26. >__________________________________________
  27. >i tried 
  28. >
  29. >return &a[n];
  30. >
  31. >in function but it doesn't work! Isn't this the right way to get the
  32. >address? (this is part of my hw, hope u don't mind) 
  33.  
  34. No, we don't mind helping people doing their homeworks, as long as they
  35. don't ask us to do them for themselves :-)
  36.  
  37. Your return statement is correct IF 'a' is an array of double which is
  38. not declared with automatic storage in function(), i.e. if its storage
  39. will continue to be allocated after the function returns (even if the
  40. array itself goes out of scope).
  41.  
  42. So, these examples are correct:
  43.  
  44. double a[SIZE];
  45. double *function()
  46. {
  47. ...
  48. return &a[n];
  49. }
  50. -------------------------
  51. double *function()
  52. {
  53. static double a[SIZE];
  54. ...
  55. return &a[n];
  56. }
  57.  
  58. but this isn't:
  59.  
  60. double *function()
  61. {
  62. double a[SIZE];
  63. ...
  64. return &a[n];
  65. }
  66.  
  67. Dan 
  68. --
  69. Dan Pop
  70. CERN, CN Division
  71. Email: danpop@mail.cern.ch 
  72. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  73.